package com.yh.workbill.ui.leaseInfo.mvp;

import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.Company;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Model;
import com.yh.workbill.ui.mvp.MainModel;

import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class AddLease_Model {

    public interface AddLeaseCallback extends IBaseModel.Fail {
        void onSuccess(String name);
    }

    interface UseCompanyCallback extends IBaseModel.Fail {
        void SuccessCompany(List<UseCompany> data);
    }


    public void delUseCompany(String company, String uuid, AddCompanyInfo_Model.DelCallBacl callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().delUseCompany(singleUserInfo.getUuid(), uuid, new BaseCallback<String>() {
                @Override
                public void accessSuccess(int code, String msg, String data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuceess(company);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                }
            });

        });
    }


    public void getAllUseCompany(int pageindex, int pagenumber, UseCompanyCallback callback) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().getAllUnitCompany(singleUserInfo.getUuid(), pageindex, pagenumber, new BaseCallback<List<UseCompany>>() {
                @Override
                public void accessSuccess(int code, String msg, List<UseCompany> data) {
                    if (code == 200) {
                        callback.SuccessCompany(data);
                    } else {
                        callback.fail(msg);
                    }
                }

                @Override
                public void accessFailure(String errorMessage) {
                    callback.fail(errorMessage);
                }
            });
        });

    }

    public void addUnitName(String work_use_company, AddLeaseCallback callback) {
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
                String phone = sp.getString("phone", "");
                UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
                UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
                OkHttpService.getInstance().postAddUnitCompanyName(singleUserInfo.getUuid(), work_use_company, new BaseCallback<String>() {
                    @Override
                    public void accessSuccess(int code, String msg, String data) {
                        ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                            if (code == 200) {
                                callback.onSuccess(work_use_company);
                            } else {
                                callback.fail(msg);
                            }
                        });
                    }

                    @Override
                    public void accessFailure(String errorMessage) {
                        ArchTaskExecutor.getMainThreadExecutor().execute(() -> callback.fail(errorMessage));
                    }
                });
            }
        });

    }
}
